﻿#region License
// (c) Intergen.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace TextGlow.Control.Model
{
  public abstract class InlineContent : Content
  {
    public new Paragraph Parent
    {
      get { return (Paragraph)base.Parent; }
    }

    public InlineContent(Paragraph parent, XElement element)
      : base(parent)
    {
      if (element == null)
        throw new ArgumentNullException("element");
    }

    public static IList<InlineContent> Create(Paragraph parent, IEnumerable<XElement> contentElements)
    {
      List<InlineContent> content = contentElements
        //.Where(e => e.Name == Run.Name || e.Name == Hyperlink.Name)
                                          .Where(e => e.Name.LocalName != "pPr")
                                          .Select(e => InlineContent.Create(parent, e))
                                          .Where(o => o != null)
                                          .ToList();

      return content;
    }

    private static InlineContent Create(Paragraph parent, XElement contentElement)
    {
      switch (contentElement.Name.LocalName)
      {
        case Run.LocalName:
          return new Run(parent, contentElement);
        case Hyperlink.LocalName:
          return new Hyperlink(parent, contentElement);
        case "proofErr":
          return null;
        case InlineStructuredTag.LocalName:
          return new InlineStructuredTag(parent, contentElement);
        case Field.LocalName:
          return new Field(parent, contentElement);
        case SmartTag.LocalName:
          return new SmartTag(parent, contentElement);
        default:
#if STRICT
          throw new ArgumentOutOfRangeException(contentElement.Name.LocalName);
#else
          return null;
#endif
      }
    }

    public abstract InlineContentType ContentType { get; }
  }
}